package cn.coco.jf.fw.admin.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.coco.jf.fw.admin.config.JkwebJson;
import cn.coco.jf.fw.admin.model.JkUser;
import cn.exlu.coco.core.utils.StringUtil;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

public class FwBaseController extends Controller {
    
    protected String getLoginId(){
        JkUser jkUser = (JkUser)this.getSessionAttr("jkUser");
        if(jkUser == null){
            return null;
        }
        
        return jkUser.getUserCode();
    }
    
    protected Integer getRoleId(){
        JkUser jkUser = (JkUser)this.getSessionAttr("jkUser");
        if(jkUser == null){
            return null;
        }
        
        return jkUser.getRoleId();
    }
    
    protected boolean validParmRequire(Serializable ... objs){
        for(Object obj : objs){
            if(obj == null){
                err("缺少参数");
                return false;
            }
        }
        return true;
    }

    protected void errCode(String errCode, String errMsg){
        setAttr("code", errCode);
        setAttr("msg", errMsg);
    }
    
    protected void errCodeJson(int errCode, String errMsg){
        setAttr("code", errCode);
        setAttr("msg", errMsg);
        renderJson();
    }
    
    protected void err(String errMsg){
        setAttr("code", 0);
        setAttr("msg", errMsg);
    }
    
    protected void errJson(String errMsg){
        setAttr("code", 0);
        setAttr("msg", errMsg);
        renderJson();
    }

    protected void success(Object obj){
        setAttr("code", 1);
        setAttr("msg", "OK");
        setAttr("obj", obj);
    }
    
    protected void successJson(Object obj){
        setAttr("code", 1);
        setAttr("msg", "OK");
        setAttr("obj", obj);
        renderJson();
    }

    protected <T> void pageJson(Page<T> page){
        setAttr("code", 1);
        setAttr("msg", "OK");
        setAttr("rows", page.getList());
        setAttr("total", page.getTotalRow());
        renderJson();
    }
    
    protected void setAttrJsonStr(String name, Object value ){
        setAttr(name, JkwebJson.toJsonStr(value));
    }

    protected String toJsonString(Object value){
        return JkwebJson.toJsonStr(value);
    }
    
    protected JSONArray parseJSONArray(String value){
        return JSONArray.parseArray(value);
    }
    
    protected JSONObject parseJSONObject(String value){
        return JSONObject.parseObject(value);
    }
    
    private List<String> parseArrs(String value){
        List<String> list = new ArrayList<String>();
        if(StringUtil.isNullOrEmpty(value)){
            return list;
        }
        
        String[] arrs = value.split(",");
        
        for(String arr : arrs){
            if(!StringUtil.isNullOrEmpty(arr)){
                list.add(arr);
            }
        }
        
        return list;
    }
    
    private List<Integer> parseIntArrs(String value){
        List<Integer> list = new ArrayList<Integer>();
        if(StringUtil.isNullOrEmpty(value)){
            return list;
        }
        
        String[] arrs = value.split(",");
        
        for(String arr : arrs){
            if(!StringUtil.isNullOrEmpty(arr)){
                list.add(Integer.valueOf(arr));
            }
        }
        
        return list;
    }
    
    protected List<Integer> getParaValuesInt(String name){
        return parseIntArrs(this.getPara(name));
    }
    
    protected List<String> getParaValuesStr(String name){
        return parseArrs(this.getPara(name));
    }
    
    /**
     * 获取参数：每页显示数量
     * @return
     */
    @Deprecated
    protected Integer getParaRows(){
        return getParaToInt("rows", 10);
    }
    
    /**
     * 获取参数：每页显示数量
     * @return
     */
    protected Integer getParaPageSize(){
        return getParaToInt("rows", 10);
    }
    
    /**
     * 获取参数：页号
     * @return
     */
    @Deprecated
    protected Integer getParaPage(){
        return getParaToInt("page", 1);
    }
    
    /**
     * 获取参数：页号
     * @return
     */
    protected Integer getParaPageNumber(){
        return getParaToInt("page", 1);
    }
}
